<?php
/**
 * File containing the BRail_Ldap_Adapter class.
 * @package BRail_Ldap
 */

/**
 * @package BRail_Ldap
 */
class BRail_Ldap_Adapter_Factory
{
    const CONFIG_FILE = 'environment.php';

    /**
     * @return BRail_Ldap_Adapter
     */
    public static function getAdapter()
    {
        static $ldapAdapter = null;
        if (!$ldapAdapter instanceof BRail_Ldap_Adapter) {
            $ldapAdapter = new BRail_Ldap_Adapter(self::getConfig());
        }
        return $ldapAdapter;
    }

    /**
     * @return array
     * @throws BRail_Ldap_Exception
     */
    private static function getConfig()
    {
        static $ldapConfig = null;
        if (! is_array($ldapConfig)) {
            $configFull = require dirname(dirname(realpath(__FILE__))) . DIRECTORY_SEPARATOR . 'config' .
                            DIRECTORY_SEPARATOR . self::CONFIG_FILE;
            $env = getenv('ENVPHP');
            if (!isset($configFull[$env])) {
                throw new BRail_Ldap_Exception(__METHOD__.' requires a valid ENVPHP.');
            }
            $ldapConfig = $configFull[$env];
        }
        return $ldapConfig;
    }
}